import requests

import config


class Order:
    # 1、登录
    def order_login(self,username, password):
        url_login = f'{config.URL}/wx/auth/login'
        headers_data = {
            'Content-Type': 'application/json'
        }
        test_data = {
            'username': username,
            'password': password
        }
        return requests.post(url=url_login, headers=headers_data, json=test_data)
    # 2、搜索
    def order_search(self,keyword):
        search_url = f'{config.URL}/wx/goods/list?keyword={keyword}&page=1&limit=10&categoryId=0'
        return requests.get(url=search_url)

    # 3、添加购物车
    def order_add_shoppingCart(self,token,add_data):
        url_login = f'{config.URL}/wx/cart/add'
        headers_data = {
            'X-Litemall-Token': token,
            'Content-Type': 'application/json'
        }
        return requests.post(url=url_login, headers=headers_data, json=add_data)


    # 4、提交订单

    def order_submit(self,token,order_data):
        url_login = f'{config.URL}/wx/order/submit'
        headers_data = {
            'X-Litemall-Token': token,
            'Content-Type': 'application/json'
        }
        return requests.post(url=url_login, headers=headers_data, json=order_data)



if __name__ == '__main__':
    # 登录
    response=Order().order_login('user123','user123')
    print(response.json())
    token = response.json().get('data').get('token')
    print(token)

    # 搜索
    response=Order().order_search('母亲节')
    print(response.json())
    goodsId = response.json().get('data').get('list')[0].get('id')
    print(goodsId)

    # 添加购物车
    add_data = {
        'goodsId': goodsId,
        'number': 5,
        'productId': 2
    }
    response=Order().order_add_shoppingCart(token,add_data)
    print(response.json())

    # 4、提交订单
    order_data = {
        "addressId": "2",
        "cartId": "0",
        "couponId": "0",
        "userCouponId": "0",
        "grouponLinkId": 0,
        "grouponRulesId": 0,
        "message": ""
    }
    response=Order().order_submit(token,order_data)
    print(response.json())